package com.fosun.spr.core.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

/**
 * 
 * @Title: ExtDateUtils.java
 * @Description: 日期处理类
 * @author zhangcheng
 * @version V1.0
 * 
 */
public class ExtDateUtils {

	private final static SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");

	private final static SimpleDateFormat sdfDay = new SimpleDateFormat(
			"yyyy-MM-dd");

	private final static SimpleDateFormat sdfDays = new SimpleDateFormat(
			"yyyyMMdd");

	private final static SimpleDateFormat sdfTime = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	/**
	 * 年月日：yyyy-MM-dd
	 */
	public static final String DATE_FORMAT = "yyyy-MM-dd";
	/**
	 * 年月日时分秒： yyyy-MM-dd HH:mm:ss
	 */
	public static final String TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss";
	
	public static final String SPLIT_FORMAT = "yyyyMMddHHmmss";
	public static SimpleDateFormat sdfhg = new SimpleDateFormat(
			"/yyyy/MM/dd/HH/");

	private ExtDateUtils() {
	}

	public static String getYear() {
		return sdfYear.format(new Date());
	}

	public static String getDay() {
		return sdfDay.format(new Date());
	}

	public static String getDays() {
		return sdfDays.format(new Date());
	}

	public static String getTime() {
		return sdfTime.format(new Date());
	}

	public static Timestamp getCurrentTimestamp() {
		return new Timestamp(System.currentTimeMillis());
	}

	public static Date getCurrentDate() {
		return new Date();
	}

	public static String formatDate(Date date, String formatStr) {

		if (date == null)
			return null;

		if (StringUtils.isBlank(formatStr))
			formatStr = DATE_FORMAT;

		final DateFormat format = new SimpleDateFormat(formatStr);
		return format.format(date);
	}

	public static Date parse(final String str, String dateFormat) {

		if (StringUtils.isBlank(dateFormat))
			dateFormat = DATE_FORMAT;

		final DateFormat format = new SimpleDateFormat(dateFormat);
		try {
			return format.parse(str);
		} catch (ParseException e) {
		}
		return null;
	}

	public static Date addYear(Date date, int years) {
		if (date == null) {
			throw new IllegalArgumentException("The date must not be null");
		}
		final Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.YEAR, years);
		return c.getTime();
	}

	public static Date addDay(Date date, int days) {
		if (date == null) {
			throw new IllegalArgumentException("The date must not be null");
		}
		final Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DAY_OF_MONTH, days);
		return c.getTime();
	}

	public static Date addHour(Date date, int hours) {
		if (date == null) {
			throw new IllegalArgumentException("The date must not be null");
		}
		final Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.HOUR_OF_DAY, hours);
		return c.getTime();
	}

	public static Date addMin(Date date, int mins) {
		if (date == null) {
			throw new IllegalArgumentException("The date must not be null");
		}
		final Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MINUTE, mins);
		return c.getTime();
	}

	public static Date addSecond(Date date, int seconds) {
		if (date == null) {
			throw new IllegalArgumentException("The date must not be null");
		}
		final Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.SECOND, seconds);
		return c.getTime();
	}

	public static int daysDifference(Date preDate, Date date) {
		final long milliSeconds = date.getTime() - preDate.getTime();
		return new Long(milliSeconds / 1000 / 3600 / 24).intValue();
	}
}
